package top.yangbuyi.controller.doctor;

import cn.hutool.core.date.DateUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.controller.BaseController;
import top.yangbuyi.domain.*;
import top.yangbuyi.dto.CareHistoryDto;
import top.yangbuyi.service.CareService;
import top.yangbuyi.service.PatientService;
import top.yangbuyi.service.RegistrationService;
import top.yangbuyi.service.impl.DeptService;
import top.yangbuyi.utils.HisDateUtils;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: CareController
 * @Author: campsis-tk
 * @Date: 2020/9/15 14:20
 * @Description: 新开就诊控制器
 **/
@RestController
@RequestMapping("doctor/care")
public class CareController extends BaseController {

	@Reference
	private RegistrationService registrationService; // 挂号服务层

	@Reference
	private PatientService patientService; // 患者服务层

	@Reference
	private CareService careService;

	@Autowired
	private DeptService deptService;

	/**
	 * @Description: 查询待就诊的挂号信息
	 * @Param: scheudlingType 就诊类型
	 * @Return: AjaxResult 成功
	 * @Author: campsis-tk
	 * @Date: 2020/9/15
	 */
	@GetMapping("queryToBeSeenRegistration/{scheudlingType}")
	public AjaxResult queryToBeSeenRegistration(@PathVariable String scheudlingType) {
		// 获取的到当前用户的部门ID
		Long deptId = ShiroSecurityUtils.getCurrentUser().getDeptId();
		// 用户ID
		Long userId = null;
		// 设置要查询的状态,状态只能为待就诊的挂号信息
		String regStatus = Constants.REG_STATUS_1;//待就诊
		// 计算时段,当前如果是上午,则接诊时只能为上午
		String subsectionType = HisDateUtils.getCurrentTimeType();
		// 查询
		List<Registration> list = this.registrationService.queryRegistration(deptId, subsectionType, scheudlingType, regStatus, userId);
		return AjaxResult.success(list);
	}

	/**
	 * @Description: 查询就诊中的挂号信息
	 * @Param: scheudlingType 就诊类型
	 * @Return: AjaxResult 成功
	 * @Author: campsis-tk
	 * @Date: 2020/9/15
	 */
	@GetMapping("queryVisitingRegistration/{scheudlingType}")
	public AjaxResult queryVisitingRegistration(@PathVariable String scheudlingType) {
		// 获取的到当前用户的部门ID
		Long deptId = ShiroSecurityUtils.getCurrentUser().getDeptId();
		// 用户ID
		Long userId = null;
		// 设置要查询的状态,状态只能为待就诊的挂号信息
		String regStatus = Constants.REG_STATUS_2;//就诊中
		// 计算时段,当前如果是上午,则接诊时只能为上午
		// String subsectionType = HisDateUtils.getCurrentTimeType();
		// 如果计算时间段 上午如果没接诊完成,下午就查询不到了
		// 则如果我上午接诊的人,我如果没接诊完成,我下午或者晚上可以继续就诊
		// 查询
		List<Registration> list = this.registrationService.queryRegistration(deptId, null, scheudlingType, regStatus, userId);
		return AjaxResult.success(list);
	}

	/**
	 * @Description: 查询就诊完成的挂号信息
	 * @Param: scheudlingType 就诊类型
	 * @Return: AjaxResult 成功
	 * @Author: campsis-tk
	 * @Date: 2020/9/15
	 */
	@GetMapping("queryVisitCompletedRegistration/{scheudlingType}")
	public AjaxResult queryVisitCompletedRegistration(@PathVariable String scheudlingType) {
		// 获取的到当前用户的部门ID
		Long deptId = ShiroSecurityUtils.getCurrentUser().getDeptId();
		// 用户ID
		Long userId = null;
		// 设置要查询的状态,状态只能为待就诊的挂号信息
		String regStatus = Constants.REG_STATUS_3;//就诊完成
		// 计算时段,当前如果是上午,则接诊时只能为上午
		// String subsectionType = HisDateUtils.getCurrentTimeType();
		// 查询
		List<Registration> list = this.registrationService.queryRegistration(deptId, null, scheudlingType, regStatus, userId);
		return AjaxResult.success(list);
	}

	/**
	 * @Description: 接诊
	 * @Param: regId 挂号单号
	 * @Return: AjaxResult
	 * @Author: campsis-tk
	 * @Date: 2020/9/15
	 */
	@PostMapping("receivePatient/{regId}")
	public AjaxResult receivePatient(@PathVariable String regId) {
		synchronized (this) { // 防止并发,防止并发接诊问题
			// 根据挂号单ID查询挂号信息
			Registration registration = this.registrationService.queryRegistrationByRegId(regId);
			if (null == registration) {
				return AjaxResult.fail("[" + regId + "]挂号单不存在,不能接诊!");
			}
			// 只有当挂号状态RegStatus为待就诊,待就诊时才可以接诊
			if (registration.getRegStatus().equals(Constants.REG_STATUS_1)) {
				// 并发问题
				registration.setRegStatus(Constants.REG_STATUS_2);// 就诊中
				// 医生ID
				registration.setUserId(ShiroSecurityUtils.getCurrentUser().getUserId());
				// 医生姓名
				registration.setDoctorName(ShiroSecurityUtils.getCurrentUser().getUserName());
				// 修改
				return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
			} else {
				return AjaxResult.fail("[" + regId + "]挂号单的挂号状态不是[待就诊],不能接诊!");
			}
		}
	}

	/**
	 * @Description: 根据患者ID获取患者信息
	 * @Param: patientId 患者ID
	 * @Return: AjaxResult
	 * @Author: campsis-tk
	 * @Date: 2020/9/15
	 */
	@GetMapping("getPatientAllMessageByPatientId/{patientId}")
	public AjaxResult getPatientAllMessageByPatientId(@PathVariable String patientId) {
		// 根据患者ID查询患者信息
		Patient patient = this.patientService.getPatientById(patientId);
		// 查询档案
		PatientFile patientFile = this.patientService.getPatientFileById(patientId);
		// 查询病例表
		List<CareHistory> careHistoryList = this.careService.queryCareHistoryByPatientId(patientId);
		HashMap<String, Object> maps = new HashMap<>();
		maps.put("patient", patient);
		maps.put("patientFile", patientFile);
		maps.put("careHistoryList", careHistoryList);
		return AjaxResult.success(maps);
	}

	/**
	 * @Description: 保存病例信息
	 * @Param: careHistoryDto
	 * @Return: AjaxResult
	 * @Author: campsis-tk
	 * @Date: 2020/9/16
	 */
	@PostMapping("saveCareHistory")
	@HystrixCommand
	public AjaxResult saveCareHistory(@RequestBody CareHistoryDto careHistoryDto) {
		careHistoryDto.setUserId(ShiroSecurityUtils.getCurrentUser().getUserId());
		careHistoryDto.setUserName(ShiroSecurityUtils.getCurrentUser().getUserName());
		careHistoryDto.setDeptId(ShiroSecurityUtils.getCurrentUser().getDeptId());
		Dept dept = this.deptService.getOne(ShiroSecurityUtils.getCurrentUser().getDeptId());
		careHistoryDto.setUserName(dept.getDeptName());
		careHistoryDto.setCaseDate(DateUtil.date());
		CareHistory careHistory = this.careService.saveOrUpdateCareHistory(careHistoryDto);
		return AjaxResult.success(careHistory);
	}
}
